#include <unistd.h>
#import "CustomTableCell.h"
#import "CDList.h"
#import "CustomTable.h"

@implementation CustomTable : UITable

// initialize the custom table objects
-(id)initWithFrame:(CGRect) rect andArray:(NSMutableArray*) a andList: (CDList *)theList
{
	self = [super initWithFrame: rect];
	_listPtr = theList;
	_dataArray = a;
	[_dataArray retain];
	
	_selectedCellIndex = -1;
	
	[self setDelegate:self];
	[self setDataSource: self];

	return self;
}

//data source routine
- (UITableCell*) table: (UITable*)table cellForRow: (int)row column: (int)col
{
	NSLog(@"customTable: table\n");
	return [_dataArray objectAtIndex: row];
}

//data source routine for size of the table
- (int) numberOfRowsInTable: (UITable*)table
{
	return [_dataArray count];
}

//attempt to allow for UNselecting a row - does not work well
- (void) tableRowSelected: (NSNotification*) notification 
{
	//if nothing was selected then allow it to be selected
	if (_selectedCellIndex == -1)
	{
		[[_dataArray objectAtIndex: [self selectedRow]] setSelected: TRUE];
		_selectedCellIndex = [self selectedRow];
	}
	// if the same cell is selected then del- select the cell
	else if( _selectedCellIndex == [self selectedRow]) 
	{
		_selectedCellIndex = -1;
		[[_dataArray objectAtIndex: [self selectedRow]] setSelected: FALSE];
// attempting to turn off the selection
//		[self selectRow: -1 byExtendingSelection: FALSE]; <- crashes app
//		[self selectRow: 2147483647 byExtendingSelection: FALSE];  <- crashes app
//		[self clearAllData];// <- works but removes all displayed data
//		[self reloadData]; // <-puts it all back as it was WITH selected row
	}
	// lastly it must be a NEW row picked so allow the movement of local variable
	else
	{
		_selectedCellIndex = [self selectedRow];
	}
	
}

//attempt to reload data and ignore those rows that have special controls
- (void) myReloadData
{
	int i;
	for(i = 0;i<[_dataArray count];i++)
	{
		if( [[self visibleCellForRow:i column:0] isRemoveControlVisible] == NO)
		{
			[ self reloadCellAtRow:i column:0 animated:FALSE];
		}
	}
}

//used to catch swipe action of the finger
- (int)swipe:(int)num withEvent:(struct __GSEvent *)event;
{
  if (num == kSwipeDirectionRight)
  {
        // The line below should be used with the newer toolchain.  If you
        // are using the older toolchain, comment this one out and use the
        // two commented out lines below.
        CGPoint point = GSEventGetLocationInWindow(event);

        // The next two lines are used with the old toolchain.  If you are
        // using the older toolchain uncomment these two lines and comment
        // out the line above.
//	CGRect rect = GSEventGetLocationInWindow(event);
//	CGPoint point = CGPointMake(rect.origin.x, rect.origin.y-50.0f); 

	CGPoint offset = _startOffset; 
	point.x += offset.x;
	point.y += offset.y;
		
	int row = [self rowAtPoint:point];
	
	if( [[self visibleCellForRow:row column:0] isRemoveControlVisible] )
	{
		[[self visibleCellForRow:row column:0] _showDeleteOrInsertion:NO withDisclosure:NO animated:NO isDelete:NO andRemoveConfirmation:NO];
	}
	else
	{
		[[self visibleCellForRow:row column:0] _showDeleteOrInsertion:YES withDisclosure:NO animated:YES isDelete:YES andRemoveConfirmation:YES];
	}
  }
  return [super swipe:num withEvent:event]; 
}

/************************************/
// return NO if you only want to move rows
- (BOOL)table:(UITable*)table canDeleteRow:( int)row 
{
	//NSLog(@"customTable: canDeleteRow: %d",row);
	return NO;
}

// this method must be declared so that rows can be moved
// if you don't want to delete rows, then return NO from table:canDeleteRow:
- (void)table:(UITable*)table deleteRow:(int)row 
{
	// the remove the counter
	[_listPtr removeRow: row];
}

// this is requires to move rows, otherwise only the delete button object is shown
- (BOOL)table:(UITable*)table canMoveRow:(int)row
{
	//NSLog(@"customTable: canMoveRow %i", row);
//	if (row == 0)
		return NO;
//	else
//		return YES;
}

//if the row is moved this is called to process the exchange
-(int)table:(UITable*)table movedRow:(int)row toRow:(int)dest
{
	//NSLog(@"customTable: movedRow:toRow: %i, %i", row, dest);
	return dest;
}

@end
